var DocData={
    userId:baseFn.getQueryString('userId',true),
    baseData:{},
    initBase:function(data){
        var that=this
        if(baseFn.notNull(that.userId)){
            for(var x in that.infoAjax){
                if(!that.infoAjax[x]){
                    return false
                }
            }
            $.get(baseUrl.base+baseUrl.sysuser+'detail',{userId:baseFn.getQueryString('userId')},function(res){
                if(res.code == '0' && res.results != null){
                    var _data = res.results
                    $('input[name=userNo]').val(_data.userNo);
                    $('input[name=userName]').val(_data.userName);
                    $('input[name=userPhone]').val(_data.userPhone);
                    $('select[name=subclinic]').val(_data.subclinicId).trigger('change');
                    $('select[name=userLevel]').val(_data.userLevel).trigger('change');
                    $('select[name=userRole]').val(_data.userRole).trigger('change');
                    $('select[name=isWork]').val(_data.isWork);
                    if(!baseFn.notNull(_data.userImg)){
                        _data.userImg = baseUrl.sysUserImg
                    }
                    $('img.imghead').attr('src',_data.userImg);
                    $('.js_newsysuser input[name=imgurl]').val(_data.userImg)
                    $('.js_newsysuser select[name=userTitle]').val(_data.userTitle).trigger('change');
                    $('.js_newsysuser select[name=workYear]').val(_data.workYear).trigger('change');
                    $('.js_newsysuser select[name=levelType]').val(_data.levelType).trigger('change');
                    this.imgChange = true
                    if(baseFn.notNull(_data.expertDesc)){
                        $('.uinfo[rel=info1]').find('textarea.js_infoTxt').val(_data.expertDesc)
                    }
                    if(baseFn.notNull(_data.workExperience)){
                        $('.uinfo[rel=info2]').find('textarea.js_infoTxt').val(_data.workExperience)
                    }
                    if(baseFn.notNull(_data.parentsSound)){
                        $('.uinfo[rel=info3]').find('textarea.js_infoTxt').val(_data.parentsSound)
                    }
                    if(baseFn.notNull(_data.learnResult)){
                        $('.uinfo[rel=info4]').find('textarea.js_infoTxt').val(_data.learnResult)
                    }
                    if(baseFn.notNull(_data.learnResultImg)){
                        var _info4Arr=_data.learnResultImg.split(',')
                        var _$4 = $('.uinfo[rel=info4]').find('.imggroup'),_$btn=_$4.find('.addbtn')
                        for(var i=0;i<_info4Arr.length;i++){ 
                            _$btn.prev('.imgupcnt').find('.Js_preview').css({'background-image':'url('+_info4Arr[i]+')'}).addClass('added')
                            _$btn.prev('.imgupcnt').find('input.Js_imgUrl').val(_info4Arr[i])
                            baseFn.imgSize({
                                src: _info4Arr[i],
                                hFn: function(res) {
                                    res.dom.css({ 'background-size': 'auto 100%' })
                                },
                                wFn: function(res) {
                                    res.dom.css({ 'background-size': '100% auto' })
                                },
                                ratio: 1,
                                otherData: {
                                    dom:_$btn.prev('.imgupcnt').find('.Js_preview')
                                }
                            })
                            if(i != _info4Arr.length-1){
                                _$btn.trigger('click');
                            }
                        }
                        //$('uinfo[rel=info4]').find('textarea.js_infoTxt').val()
                    }
                    if(baseFn.notNull(_data.proAuth)){
                        $('.uinfo[rel=info5]').find('textarea.js_infoTxt').val(_data.proAuth)
                    }
                    if(baseFn.notNull(_data.proAuthImg)){
                        var _info5Arr=_data.proAuthImg.split(',')
                        var _$5 = $('.uinfo[rel=info5]').find('.imggroup'),_$btn=_$5.find('.addbtn')
                        for(var j=0;j<_info5Arr.length;j++){
                            _$btn.prev('.imgupcnt').find('.Js_preview').css({'background-image':'url('+_info5Arr[j]+')'}).addClass('added')
                            _$btn.prev('.imgupcnt').find('input.Js_imgUrl').val(_info5Arr[j])
                            baseFn.imgSize({
                                src: _info5Arr[j],
                                hFn: function(res) {
                                    res.dom.css({ 'background-size': 'auto 100%' })
                                },
                                wFn: function(res) {
                                    res.dom.css({ 'background-size': '100% auto' })
                                },
                                ratio: 1,
                                otherData: {
                                    dom:_$btn.prev('.imgupcnt').find('.Js_preview')
                                }
                            })
                            if(j != _info5Arr.length-1){
                                _$btn.trigger('click');
                            }
                        }
                        //$('uinfo[rel=info5]').find('textarea.js_infoTxt').val()
                    }
                }
            },'json')
        }
    },
    imgChange:false,
    infoAjax:{
        subclinic:false,
        sys:false
    },
    subclinicId:'',
    addSending:false//提交的判定
}
//预约列表 tab切换
$('.bespeak_change_list_actve').on('click', function () {
    var thisIndex = $(this).index();
    $(this).addClass("active_bgc").siblings().removeClass("active_bgc");
    $('.js_tabcnt').eq(thisIndex).removeClass('hidden').siblings(".js_tabcnt").addClass('hidden');
})
/*基础设置*/
baseFn.rpSelect('.js_subclinic',{
    url:baseUrl.base+baseUrl.subclinic+'selectSubclinicList',
    idKey:'subclinicId',
    textKey:'subclinicName',
    calFn:function(){
        DocData.infoAjax.subclinic=true
        DocData.initBase()
        //权限管理 45 全部门店
        if(SessionData.powerSub){
            $('.js_subclinic').val(SessionData.powerSub).trigger('change')
        }
    }
})
$.get(baseUrl.base+baseUrl.init+'selectSysList',{},function(res){
    if(res.code == 0){
        baseFn.rpSelect('.js_userRole',{
            datas:res.results.roleList,
            idKey:'roleId',
            textKey:'roleName'
        })
        baseFn.rpSelect('.js_userLevel',{
            datas:res.results.gradeList,
            idKey:'gradeId',
            textKey:'gradeName'
        })
        baseFn.rpSelect('.js_titleLabel',{
            datas:res.results.titleLabelList,
            idKey:'labelName',
            textKey:'labelName'
        })
        baseFn.rpSelect('.js_yearLabel',{
            datas:res.results.yearLabelList,
            idKey:'labelName',
            textKey:'labelName'
        })
        baseFn.rpSelect('.js_levelLabel',{
            datas:res.results.levelLabelList,
            idKey:'labelName',
            textKey:'labelName'
        })
        DocData.infoAjax.sys=true
        DocData.initBase()
    }
},'json')


$('.imghead').on('click',function(){
    $('#Js_previewImgNew').trigger('click')
})
//图片上传{}
function zcuploadImageCover(e){
    baseFn.imgUpload($(e).parents('form').attr('id'),function(res){
        $(e).siblings('.Js_preview').find('img').attr('src',res.url)
        $('input[name=imgurl]').val(res.url)
        DocData.imgChange=true
    })
}
var myValidform = $(".js_newsysuser").Validform({
    tiptype:2,
    datatype:{
        putso:function(gets,obj,curform,regxp){
            /*参数gets是获取到的表单元素值，
              obj为当前表单元素，
              curform为当前验证的表单，
              regxp为内置的一些正则表达式的引用。*/
            var _checkpwrap = obj.parents('.checkpwrap')
            var _type = _checkpwrap.find('.checkp').val()
            var _userid = _checkpwrap.find('.userid').val()
            var _post = _checkpwrap.find('.post').val()
            var reg1=regxp["*"],
                reg2=/[\d]{7}/,
                mobile=curform.find(".mobile");
            if(obj){
                if(_type == '1'){
                    //人
                    //console.log(_userid)
                    if(reg1.test(_userid)){return true;}
                }else if(_type == '3'){
                    //岗位
                    if(reg1.test(_post)){return true;}
                }
            }
            return false;
        },
        ztreegroup:function(gets,obj,curform,regxp){
            console.log(gets)
            if(gets == ''){
                return false
            }else{
            }
        },
        select2:function(gets,obj,curform,regxp){
            if(gets.length ==0){
                obj.siblings('.select2').find('.select2-selection').css('borderColor','red')
                return false
            }else{
                obj.siblings('.select2').find('.select2-selection').css('borderColor','#ccc')
            }
        }
    }
}); 
$('.js_newsysuser .edit_ok').on('click',function(){
    if(!myValidform.check()){
        return false
    }
    var _upDatas={
        userNo:$('input[name=userNo]').val(),
        userName:$('input[name=userName]').val(),
        userPhone:$('input[name=userPhone]').val(),
        subclinicId:$('select[name=subclinic]').val(),
        userLevel:$('select[name=userLevel]').val(),
        userTitle:$('.js_newsysuser select[name=userTitle]').val(),
        workYear:$('.js_newsysuser select[name=workYear]').val(),
        levelType:$('.js_newsysuser select[name=levelType]').val(),
        isWork:$('.js_newsysuser select[name=isWork]').val()
    }
    //权限管理 44 职位权限
    if(!baseFn.getPower(44)){
        _upDatas.userRole=$('select[name=userRole]').val()
    }
    var _url = baseUrl.base+baseUrl.sysuser+'add',_text='成功添加员工！'
    
    if(DocData.imgChange){
        _upDatas.userImg=$('input[name=imgurl]').val()
    }
    if(DocData.userId != ''){
        _upDatas.userId = DocData.userId
        _url = baseUrl.base+baseUrl.sysuser+'modify'
        _text = '成功修改员工！'
    }
    if(DocData.addSending){
        baseFn.infoDialog('正在提交，请稍后重试');
        return false
    }else{
        DocData.addSending = true
    }
    $.post(_url,_upDatas,function(res){
        DocData.addSending = false
        if(res.code == '0'){
            // baseFn.infoDialog(_text,function(){
            //     window.location.href = baseUrl.gobase+'sysuser/gotoCtaffManagement'
            // })
            baseFn.dialog({
                title:"成功",
                content:_text+'是否返回员工列表？',
                ok:function(res){
                    window.location.href = baseUrl.gobase+'sysuser/gotoCtaffManagement'
                    DocData.userId = res.results.userId
                },
                cancel:function(){}
            })
        }else{
            baseFn.infoDialog(res.msg)
        }
    },'json').complete(function(){
        DocData.addSending = false
    })
})
$('.js_newsysuser .edit_cancel').on('click',function(){
    window.location.href = baseUrl.gobase+'sysuser/gotoCtaffManagement'
})

/*基础设置*/
/*其他信息设置*/

function upinfoimg(e){
    baseFn.imgUpload($(e).parents('form').attr('id'),function(res){
        $(e).siblings('.Js_preview').css({ "background-image": 'url(' + res.url + ')' })
        $(e).parents('form').siblings('input.Js_imgUrl').val(res.url)
        baseFn.imgSize({
            src: res.url,
            hFn: function(res) {
                res.dom.css({ 'background-size': 'auto 100%' })
            },
            wFn: function(res) {
                res.dom.css({ 'background-size': '100% auto' })
            },
            ratio: 1,
            otherData: {
                dom:$(e).siblings('.Js_preview')
            }
        })
        $(e).siblings('.Js_preview').addClass('added')
    })
}
function delimg(e){
    $(e).parents('.imgupcnt').remove()
}
$('.imggroup').on('click','.Js_preview',function(){
    $(this).siblings('input[name=img]').trigger('click')
})
$('.addbtn').on('click',function(){
    var $this=$(this),_type = $this.parents('.uinfo').attr('rel')
    var _imgupdom = '<div class="imgupcnt">\
            <form class="Js_uploadForm uploadform" method="post" enctype="multipart/form-data" id="'+_type+'_up_form'+(Number($this.data('step'))+1)+'">\
                <div class="Js_preview preview" style="background-image: url(&quot;&quot;);"><em onclick="delimg(this)"> - </em> </div>\
                <input type="file" name="img" onchange="upinfoimg(this)" style="display: none;">\
            </form>\
            <input type="hidden" class="Js_imgUrl" name="imgurl" value="" datatype="*" nullmsg="必须上传图片">\
        </div>'
        $this.before(_imgupdom)
        $this.data('step',Number($this.data('step'))+1)
})
$('.js_newsysuserinfo .edit_info_ok').on('click',function(){
    if(!baseFn.notNull(DocData.userId)){
        baseFn.infoDialog('请将基本信息填写完整并保存！');
        return false
    }
    var _upDatas={
        userId:DocData.userId
    }
    var _url = baseUrl.base+baseUrl.sysuser+'modify',_text='员工其他信息保存成功！'

    var _map={
        info1Txt:'expertDesc',
        info2Txt:'workExperience',
        info3Txt:'parentsSound',
        info4:'learnResultImg',
        info4Txt:'learnResult',
        info5:'proAuthImg',
        info5Txt:'proAuth'
    }
    $('.uinfo').each(function(v,n){
        var $this=$(n),_mapkey = $this.attr('rel')
        if(_map[_mapkey]+'Txt' &&　baseFn.notNull($this.find('.js_infoTxt').val())){
            _upDatas[_map[_mapkey+'Txt']]=$this.find('.js_infoTxt').val()
        }
        if(_map[_mapkey]){
            var _arr = []
            $this.find('.imgupcnt').each(function(s,m){
                if(baseFn.notNull($(m).find('input[name=imgurl]').val())){
                    _arr.push($(m).find('input[name=imgurl]').val())
                }
            })
            _upDatas[_map[_mapkey]]=_arr.join(',')
        }
        
    })
    $.post(_url,_upDatas,function(res){
        if(res.code == '0'){
            baseFn.dialog({
                title:"成功",
                content:_text+'是否返回员工列表？',
                ok:function(){
                    window.location.href = baseUrl.gobase+'sysuser/gotoCtaffManagement'
                },
                cancel:function(){}
            })
            // baseFn.infoDialog(_text,function(){
            //     window.location.href = baseUrl.gobase+'sysuser/gotoCtaffManagement'
            // })
        }
    },'json')
})

/*其他信息设置*/
$('.js_newsysuser').css('height',windowOffsetData.mainWraperHeight-160)
$('.js_newsysuserinfo').css('height',windowOffsetData.mainWraperHeight-160)
window.onresize = function(){
    windowOffsetData.refresh();
    $('.js_newsysuser').css('height',windowOffsetData.mainWraperHeight-160)
    $('.js_newsysuserinfo').css('height',windowOffsetData.mainWraperHeight-160)
}